package com.ruoyi.common.utils.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.*;
import java.util.Properties;

@Component
public class JedisPoolUtils {

    @Value("${redis.maxIdle}")
    private Integer maxIdle;

    @Value("${redis.maxWait}")
    private Integer maxWait;

    @Value("${redis.minIdle}")
    private Integer minIdle;

    @Value("${redis.maxTotal}")
    private Integer maxTotal;

    @Value("${redis.port}")
    private Integer port;

    @Value("${redis.url}")
    private String url;

    //获得jedis资源的方法
    public Jedis getJedis() {
        JedisPool pool = null;
        //获得池子对象
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);//最大闲置个数
        poolConfig.setMaxWaitMillis(maxWait);//最大闲置个数
        poolConfig.setMinIdle(minIdle);//最小闲置个数
        poolConfig.setMaxTotal(maxTotal);//最大连接数
        poolConfig.setBlockWhenExhausted(true);
        poolConfig.setTestOnCreate(true);
        poolConfig.setTestOnBorrow(false);
        poolConfig.setTestOnReturn(false);
        pool = new JedisPool(poolConfig, url, port);
        return pool.getResource();
    }

}
